package com.jsl.action;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONValidator;
import com.jsl.NCCUtil;
import com.jsl.NCCloud21Service;
import com.jsl.NCCloudService;
import com.jsl.ResultInfo;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.request.RequestManager;

import java.util.HashMap;
import java.util.Map;

public class AddMaterialAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo info) {
        writeLog("**********************进入 AddMaterialAction **********************");

        String requestid = info.getRequestid();
        String workflowid = info.getWorkflowid();
        RequestManager requestManager = info.getRequestManager();
        String formtable = NCCUtil.getFormtable(workflowid);

        writeLog("[AddMaterialAction.execute] workflowid:" + workflowid + " formtable:" + formtable + " requestid:" + requestid);

        String requestBody = "";
        String result = ""; // 返回结果
        String sql = "";
        boolean flag = true;
        RecordSet recordSet = new RecordSet();
        try {
            sql = "select * from " + formtable + " where requestid=" + requestid;
            recordSet.execute(sql);
            if (recordSet.next()) {
                // 从OA流程中提取参数
                String materialName = recordSet.getString("material_name"); // 物料名称
                String materialspec = recordSet.getString("wlgg"); // 物料规格
                String pkMarbasclass = recordSet.getString("wlflsxllk"); // 物料分类
                if (pkMarbasclass.contains("4_")) {
                    pkMarbasclass = pkMarbasclass.substring(2);
                }
                String pkMeadoc = recordSet.getString("wldwllk"); // 物料单位
                String materialCode = recordSet.getString("scdwlbm"); // 物料单位
                String memo = recordSet.getString("wlbz"); // 物料备注
                // 构造请求体的map参数对象
                Map<String, Object> paramsMap = new HashMap<>();
                // ufinterface参数
                Map<String, Object> ufinterfaceMap = new HashMap<>();
                ufinterfaceMap.put("account", "01");
                ufinterfaceMap.put("billtype", "material");
                ufinterfaceMap.put("isexchange", "Y");
                ufinterfaceMap.put("replace", "Y");
                ufinterfaceMap.put("sender", "default");
                ufinterfaceMap.put("groupcode", "HBJSL");
                // bill参数
                Map<String, Object> billMap = new HashMap<>();
                billMap.put("billhead", createBillHeadMap(materialCode, materialName, materialspec, "", memo, pkMarbasclass, pkMeadoc));
                ufinterfaceMap.put("bill", billMap);
                paramsMap.put("ufinterface", ufinterfaceMap);
                // 构造请求体转换为json
                requestBody = JSON.toJSONString(paramsMap);
                writeLog("[AddMaterialAction.execute] requestBody:" + requestBody);
                String apiUrl = "/nccloud/api/uapbd/material/material/add";
                // 拿到返回数据，传入apiUrl、请求参数
                result = NCCloud21Service.pushData(apiUrl, requestBody);
                // 记录返回结果
                writeLog("[AddMaterialAction.execute] result:" + result);
            }

        } catch (Exception e) {
            flag = false;

            writeLog(e);

        }
        return null;
    }


    private static Map<String, Object> createBillHeadMap(String materialCode, String materialName, String materialspec, String materialtype, String memo, String pkMarbasclass, String pkMeasdoc) {
        Map<String, Object> billHeadMap = new HashMap<>();
        billHeadMap.put("pk_group", "0001M110000000003JBD");
        billHeadMap.put("pk_org", "0001M110000000003JBD");
        billHeadMap.put("code", materialCode);
        billHeadMap.put("name", materialName);
        billHeadMap.put("materialspec", materialspec);
        billHeadMap.put("materialtype", materialtype);
        billHeadMap.put("memo", memo);
        billHeadMap.put("ename", materialName);
        billHeadMap.put("pk_mattaxes", "1001Z01000000003W0WH");
        // 出入库容差
        billHeadMap.put("intolerance", "0.0000");
        billHeadMap.put("outcloselowerlimit", "0.0000");
        billHeadMap.put("outtolerance", "0.0000");
        // 物料基本分类
        billHeadMap.put("pk_marbasclass", pkMarbasclass);
        // 物料单位
        billHeadMap.put("pk_measdoc", pkMeasdoc);
        billHeadMap.put("pk_eps", "1001A1100000000NABIM");
        // 启用状态
        billHeadMap.put("enablestate", "2");
        // 产品族
        billHeadMap.put("productfamily", "N");

        Map<String, Object> materialconvertMap = new HashMap<>();
        materialconvertMap.put("item", createItemMap(pkMeasdoc));
        billHeadMap.put("materialconvert", materialconvertMap);

        Map<String, Object> materialtaxtypeMap = new HashMap<>();
        materialtaxtypeMap.put("item", createItemMap2());
        billHeadMap.put("materialtaxtype", materialtaxtypeMap);

        return billHeadMap;
    }

    /**
     * @return
     */
    private static Map<String, Object> createItemMap(String pk_measdoc) {
        Map<String, Object> itemMap = new HashMap<>();
        itemMap.put("pk_measdoc", pk_measdoc);
        itemMap.put("measrate", "1/1");
        return itemMap;
    }

    /**
     * @return
     */
    private static Map<String, Object> createItemMap2() {
        Map<String, Object> itemMap = new HashMap<>();
        itemMap.put("pk_countryzone", "CN");
        itemMap.put("pk_mattaxes", "CN01");
        return itemMap;
    }
}
